package com.iotek.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.iotek.entity.Customer;
import com.iotek.service.CustomerService;



@Controller
@RequestMapping("/Customer")
public class CustomerController {
	@Autowired
	private CustomerService customerServiceImpl;
	
	@RequestMapping("/login.action")
	public String login(HttpServletRequest request,Customer customer,Model model) {
		String customerPhone = request.getParameter("customerPhone");
		String password = request.getParameter("password");
		customer.setCustomerPhone(customerPhone);
		customer.setPassword(password);
		Customer cu =customerServiceImpl.login(customer);
		System.out.println(cu);
		String customerName = cu.getCustomerName();
		model.addAttribute("customerName", customerName);
		if(cu != null) {
			HttpSession session = request.getSession();
			session.setAttribute("customer", cu);
			return "information";
		}else {
			return "error";
		}
	}
	@RequestMapping("/register.action")
	public String register(HttpServletRequest req,Customer customer) {
		String customerPhone = req.getParameter("customerPhone");
		String password = req.getParameter("password");
		customer.setCustomerPhone(customerPhone);
		customer.setPassword(password);
		customerServiceImpl.addCustomer(customer);
		return "login";
		
	}
	
}
